package com.jizhi.onework.framework.shiro.service;


import com.jizhi.onework.dao.UserMapper;
import com.jizhi.onework.dataobj.vo.UserVO;
import com.jizhi.onework.framework.constants.Constant;
import com.jizhi.onework.utils.JwtUtil;
import com.jizhi.onework.framework.constants.UserConstants;
import com.jizhi.onework.framework.context.OperationContext;
import com.jizhi.onework.framework.context.OperationContextHolder;
import com.jizhi.onework.framework.exception.UserNotExistsException;
import com.jizhi.onework.framework.exception.UserPasswordNotMatchException;
import com.jizhi.onework.pojo.User;
import com.jizhi.onework.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 登录校验方法
 *
 * @author hzhang7
 */
@Component
public class LoginService {

    @Autowired
    private PasswordService passwordService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     */
    public User login(String username, String password) {

        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new UserNotExistsException();
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            throw new UserPasswordNotMatchException();
        }

        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            throw new UserPasswordNotMatchException();
        }

        // 查询用户信息
        User user = userMapper.selectUserByUserName(username);

        if (user == null && maybeMobilePhoneNumber(username)) {
            user = userMapper.selectUserByPhoneNumber(username);
        }

        if (user == null && maybeEmail(username)) {
            user = userMapper.selectUserByEmail(username);
        }

        if (user == null) {
            throw new UserNotExistsException();
        }
        passwordService.validate(user, password);
        createToken(user);

        return user;
    }

    private void createToken(User user) {
        // 生成jwtToken
        String jwtToken = JwtUtil.sign(user.getPhone(), user.getId(), user.getPassword());
        UserVO userVO = new UserVO(user);
        userVO.setToken(jwtToken);

        // 将当前登录用户放入 ThreadLocal 中
        OperationContextHolder.clear();
        OperationContextHolder.set(new OperationContext(userVO));
        RedisUtil.set(jwtToken, jwtToken, Constant.TOKEN_EXPIRE_TIME);
    }

    private boolean maybeEmail(String username) {
        if (!username.matches(UserConstants.EMAIL_PATTERN)) {
            return false;
        }
        return true;
    }

    private boolean maybeMobilePhoneNumber(String username) {
        if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN)) {
            return false;
        }
        return true;
    }

}